package com.example.boss.pinkdiary;

import com.example.boss.db.gen.DaoMaster;
import com.example.boss.db.gen.DaoSession;
import com.example.boss.db.gen.diaryList;
import com.example.boss.db.gen.diaryListDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by 打好boss on 2018/6/13.
 */

public class diaryListDaoHelper {

    static private diaryListDao dld;
    public diaryListDaoHelper(diaryListDao dld){

        this.dld=dld;
    }
    public static List<diaryList> orderByDate_Desc(int temp)
    {
        if(temp==0){
            return  dld.queryBuilder()
                    .where(diaryListDao.Properties.Lock.eq ("true"))
                    .orderDesc(diaryListDao.Properties.Date)
                    .list();
        }
        else{
            return  dld.queryBuilder()
                    .orderDesc(diaryListDao.Properties.Date)
                    .list();
        }

    }
    public static List<diaryList> orderByDate_Asc(int temp)
    {
        if(temp==0){
            return  dld.queryBuilder()
                    .where(diaryListDao.Properties.Lock.eq ("true"))
                    .orderAsc(diaryListDao.Properties.Date)
                    .list();
        }
        else{
            return  dld.queryBuilder()
                    .orderAsc(diaryListDao.Properties.Date)
                    .list();
        }
    }
    public static List<diaryList> select_private(int temp)
    {
        if(temp==0){
            return  dld.queryBuilder()
                    .orderAsc(diaryListDao.Properties.Date)
                    .list();
        }
        else{
            return  dld.queryBuilder()
                    .orderDesc(diaryListDao.Properties.Date)
                    .list();
        }
    }
    public static List<diaryList> select_no_private(int temp)
    {
        if(temp==0){
            return  dld.queryBuilder()
                    .where(diaryListDao.Properties.Lock.eq ("true"))
                    .orderAsc(diaryListDao.Properties.Date)
                    .list();
        }
        else{
            return  dld.queryBuilder()
                    .where(diaryListDao.Properties.Lock.eq ("true"))
                    .orderDesc(diaryListDao.Properties.Date)
                    .list();
        }
    }
    public static List<diaryList> selectByDate(String date)
    {
        return  dld.queryBuilder()
                .where(diaryListDao.Properties.Date.eq(date))
                .list();
    }
    public static  void deleteByDate(String date){
        dld.queryBuilder().where(diaryListDao.Properties.Date.eq(date)).buildDelete().executeDeleteWithoutDetachingEntities();
    }
    public static List<diaryList> selectByKey(String key,int temp1,int temp2){
        if(temp1==0){
            if(temp2==0){//asc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Lock.eq ("true"),
                qb.or(diaryListDao.Properties.Title.like("%"+key+"%"), diaryListDao.Properties.Content.like("%"+key+"%")));
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//asc private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(qb.or(diaryListDao.Properties.Title.like("%"+key+"%"), diaryListDao.Properties.Content.like("%"+key+"%")));
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
        else{
            if(temp2==0){//desc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Lock.eq ("true"),
                        qb.or(diaryListDao.Properties.Title.like("%"+key+"%"), diaryListDao.Properties.Content.like("%"+key+"%")));
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//desc private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(qb.or(diaryListDao.Properties.Title.like("%"+key+"%"), diaryListDao.Properties.Content.like("%"+key+"%")));
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
    }
    public static List<diaryList> selectByTimeKey(String timeKey,int temp1,int temp2){
        String key1="",key2="";
        String[] k;
        if(timeKey.indexOf("之前")!=-1){
            k=timeKey.split("之前");
            String temp=k[0];
            if(temp.indexOf("日")!=-1){
                key1=temp+"/00:00:00";
            }
            else{
                if(temp.indexOf("月")!=-1) key1=temp+"0日/00:00:00";
                else key1=temp+"/0月0日/00:00:00";
            }
        }
        else{
            key2=timeKey;
        }
        if(!key1.equals("")){
            if(temp1==0){
                if(temp2==0){//asc no_private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Date.le(key1)));
                    qb.orderAsc(diaryListDao.Properties.Date);
                    return qb.list();
                }
                else{//asc private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(diaryListDao.Properties.Date.le(key1));
                    qb.orderAsc(diaryListDao.Properties.Date);
                    return qb.list();
                }
            }
            else{
                if(temp2==0){//desc no_private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Date.le(key1)));
                    qb.orderDesc(diaryListDao.Properties.Date);
                    return qb.list();
                }
                else{//desc private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(diaryListDao.Properties.Date.le(key1));
                    qb.orderDesc(diaryListDao.Properties.Date);
                    return qb.list();
                }
            }
        }
        else{
            if(temp1==0){
                if(temp2==0){//asc no_private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Date.like("%"+key2+"%")));
                    qb.orderAsc(diaryListDao.Properties.Date);
                    return qb.list();
                }
                else{//asc private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(diaryListDao.Properties.Date.like("%"+key2+"%"));
                    qb.orderAsc(diaryListDao.Properties.Date);
                    return qb.list();
                }
            }
            else{
                if(temp2==0){//desc no_private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Date.like("%"+key2+"%")));
                    qb.orderDesc(diaryListDao.Properties.Date);
                    return qb.list();
                }
                else{//desc private
                    QueryBuilder qb = dld.queryBuilder();
                    qb.where(diaryListDao.Properties.Date.like("%"+key2+"%"));
                    qb.orderDesc(diaryListDao.Properties.Date);
                    return qb.list();
                }
            }
        }

    }
    public static List<diaryList> select(int temp1,int temp2){
        if(temp1==0){
            if(temp2==0){//asc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Lock.eq ("true"));
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//asc private
                QueryBuilder qb = dld.queryBuilder();
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
        else{
            if(temp2==0){//desc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Lock.eq ("true"));
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//desc private
                QueryBuilder qb = dld.queryBuilder();
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
    }
    public List<diaryList> selectByTag(String tag,int temp1,int temp2){

        if(temp1==0){
            if(temp2==0){//asc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Tag.eq (tag)));
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//asc private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Tag.eq (tag));
                qb.orderAsc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
        else{
            if(temp2==0){//desc no_private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(qb.and(diaryListDao.Properties.Lock.eq ("true"),diaryListDao.Properties.Tag.eq (tag)));
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
            else{//desc private
                QueryBuilder qb = dld.queryBuilder();
                qb.where(diaryListDao.Properties.Tag.eq (tag));
                qb.orderDesc(diaryListDao.Properties.Date);
                return qb.list();
            }
        }
    }
}

